package com.runyun.project.controller;

import com.alibaba.fastjson.JSONArray;
import com.runyun.project.entity.*;
import com.runyun.project.mapper.FileMapper;
import com.runyun.project.mapper.FollowMapper;
import com.runyun.project.mapper.ShareMapper;
import com.runyun.project.mapper.UserMapper;
import com.runyun.project.util.RunYunUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FriendController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private FollowMapper followMapper;

    @GetMapping("/getFriends/{userId}")
    public String getFriends(@PathVariable("userId") Integer userId) {
        System.out.println("userId ==== " + userId);

        List<UserShow> list = getFriendInfo(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        return RunYunUtils.getJSONString(200, "查询用户", map);
    }

    @PostMapping("/deleteFriends/{userId}")
    public String getFriends(@PathVariable("userId") Integer userId, @RequestBody String users) {
        List<UserShow> list = JSONArray.parseArray(users,UserShow.class);
        System.out.println(list);

        for (UserShow userShow: list) {
            User user = userMapper.getUserByEmail(userShow.getEmail());
            followMapper.deleteFollow(userId, user.getUserId());
        }
        List<UserShow> userShows = getFriendInfo(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("data", userShows);
        return RunYunUtils.getJSONString(200, "删除用户成功！", map);
    }

    @PostMapping("/addFriend/{userId}")
    public String addFriend(@PathVariable("userId") Integer userId, @RequestBody String email_) {
        String email = email_.substring(0, email_.length()-1).replace("%40", "@");
        System.out.println("email: " + email);
        User user = userMapper.getUserByEmail(email);
        System.out.println(user);
        if (user == null) {
            return RunYunUtils.getJSONString(400, "用户不存在！");
        }
        // 将用户关注信息填入数据库
        Follow follow = new Follow();
        follow.setUserId(userId);
        follow.setFollowedId(user.getUserId());
        followMapper.insertFollow(follow);
        Map<String, Object> map = new HashMap<>();
        List<UserShow> list = getFriendInfo(userId);
        map.put("data", list);
        return RunYunUtils.getJSONString(200, "添加成功", map);
    }

    private List<UserShow> getFriendInfo(Integer userId) {
        List<Integer> userIds = followMapper.queryByUserId(userId);

        List<UserShow> list = new ArrayList<>();
        for (Integer id: userIds) {
            User user = userMapper.queryById(id);
            UserShow userShow = new UserShow();
            userShow.setAUserName(user.getUserName());
            userShow.setEmail(user.getEmail());
            list.add(userShow);
        }
        return list;
    }

}
